package com.test.springboot.qiniukodo.service.impl;

import com.qiniu.util.StringUtils;
import com.test.springboot.qiniukodo.service.QiniuKodoService;
import com.test.springboot.qiniukodo.utils.QiniuKodoUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;


/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/8/10 10:54
 * @package com.springboot.qiniukode.service
 * @description 七牛云Kodo服务实现类
 */


@Service
public class QiniukodoServiceImpl implements QiniuKodoService {

    @Override
    public String uploadPath(String localFilePath, String fileName, boolean override) {
        // 判断本地文件是否为空
        if (StringUtils.isNullOrEmpty(localFilePath)) {
            throw new RuntimeException("文件路径为空");
        }

        return QiniuKodoUtil.uploadFile(localFilePath, fileName, override);
    }

    @Override
    public String uploadBytes(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID() + "-" + originalFilename;
        if (file.isEmpty()) {
            throw new RuntimeException("文件为空");
        }
        try {
            byte[] bytes = file.getBytes();
            return QiniuKodoUtil.uploadFile(bytes, fileName);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public String uploadStream(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID() + "-" + originalFilename;
        if (file.isEmpty()) {
            throw new RuntimeException("文件为空");
        }
        try {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(file.getBytes());
            return QiniuKodoUtil.uploadFile(byteArrayInputStream, fileName);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public String resumableUpload(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID() + "-" + originalFilename;
        if (file.isEmpty()) {
            throw new RuntimeException("文件为空");
        }
        try {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(file.getBytes());
            return QiniuKodoUtil.resumableUploadFile(byteArrayInputStream, fileName);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public String download(String fileName) throws UnsupportedEncodingException {
        if (StringUtils.isNullOrEmpty(fileName)) {
            throw new RuntimeException("文件名为空");
        }
        return QiniuKodoUtil.getFileUrl(fileName);
    }

    @Override
    public String privateDownload(String fileName) throws UnsupportedEncodingException {
        if (StringUtils.isNullOrEmpty(fileName)) {
            throw new RuntimeException("文件名为空");
        }
        return QiniuKodoUtil.getPrivateFileUrl(fileName);
    }

    @Override
    public String delete(String fileName) {
        if (StringUtils.isNullOrEmpty(fileName)) {
            throw new RuntimeException("文件名为空");
        }
        return QiniuKodoUtil.delete(fileName);
    }

    @Override
    public String getUpToken() {
        return QiniuKodoUtil.getUpToken();
    }

    @Override
    public String getCustomUpToken() {
        return QiniuKodoUtil.getCustomUpToken();
    }

    @Override
    public String getUpTokenWithCallback() {
        return QiniuKodoUtil.getUpTokenWithCallback();
    }
}

